<template>
  <div>
    <van-cell-group>
      <van-field
        v-model="tel"
        label="手机号"
        placeholder="请输入手机号"
        autofocus
      />
    </van-cell-group>
    <div class="loginBtn">
      <van-button
        type="danger"
        round
        size="large"
        :disabled="flag"
        @click="next"
        >下一步</van-button
      >
    </div>
  </div>
</template>

<script>
import { doCheckPhone } from "../../api/user";
import Vue from "vue";
import { Dialog } from "vant";
Vue.use(Dialog);
export default {
  data() {
    return {
      tel: "",
      //flag: true,//flag是不是需要依赖我们的tel来计算它的true和false
    };
  },
  computed: {
    flag() {
      //满足输入内容满足手机号的正则规则
      if (/^1[3456789]\d{9}$/.test(this.tel)) {
        return false; //按钮显示红色正常可以
      } else {
        return true; //暗色显示禁用
      }
    },
  },
  methods: {
    next() {
      //验证你的tel是否可以注册，连接口
      doCheckPhone({ tel: this.tel }).then((res) => {
        console.log(res.data.code);
        switch (res.data.code) {
          case "10005":
            Dialog.confirm({
              message: "用户手机号已注册，是否去登录",
            })
              .then(() => {
                // on confirm 点了确定
                this.$router.push("/login");
              })
              .catch(() => {
                // on cancel 点了取消
              });
            break;
          case "200":
            localStorage.setItem("tel",this.tel)
            this.$router.push("/register/two");
            break;
          default:
            break;
        }
      });
    },
  },
};
</script>

<style scoped>
.loginBtn {
  width: 90%;
  margin: 20px auto;
}
</style>